package com.live.video.controller;

import com.live.video.enums.ResultEnum;
import com.live.video.model.RedPacket;
import com.live.video.model.User;
import com.live.video.server.RedPacketServer;
import com.live.video.server.UserServer;
import com.live.video.util.Result;
import com.live.video.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

/**
 * Description:
 * User: godd
 * Date: 2017-11-14
 * Time: 21:31
 */
@RestController
@Slf4j
@RequestMapping("red")
public class RedPacketController {

    @Autowired
    private RedPacketServer server;

    @Autowired
    @Lazy
    private UserServer userServer;

    /**
     * 直播间发起红包并且发送到直播间
     *
     * @author 戴长春    dcc@vtc365.com
     * @date 2017/11/14 21:52
     */
    @RequestMapping("/create")
    Result create(@RequestBody(required = true) RedPacket packet) {
        server.save(packet);
        //判断是否保存成功
        if (packet.getId() != null) {
            server.pushMsgToRoom(packet);
            return ResultUtil.success(packet.getId());
        }
        return ResultUtil.error(ResultEnum.RED_PACKET_CREATE_FAILED);
    }

    /**
     * 抢红包实现
     *
     * @author 戴长春    dcc@vtc365.com
     * @date 2017/11/14 23:54
     */
    @RequestMapping("/grab/{id}")
    Result grab(@PathVariable(required = true) Integer id, @RequestBody User user) {
        user = userServer.queryOne(user);
        server.grapRedPacket(id, user);
        return ResultUtil.success();
    }
}
